package com.autonavi.yunda.yunji.groovy.test

import org.junit.jupiter.api.Test

import java.util.stream.Collectors

/**
 * @author cizhong.wcs
 * @date 2023/1/9 19:33
 */
/**
 * @author cizhong.wcs
 * @date 2023/1/6 18:37
 */
class Groovy4Test {
    @Test
    void testGroovy4() {
        println 'hello groovy'
        testStream()
        testComplexSwitchCase()
        testRange()
    }

    static def testStream() {
        def reli = [["xy": "103.907799157 37.5445544554", "index": 988.9], ["xy": "101.907799157 37.5445544554", "index": 111], ["xy": "113.907799157 37.5445544554", "index": 999]]
        assert ['A', 'B', 'C'] == ['a', 'b', 'c'].stream().map(String::toUpperCase).collect(Collectors.toList())
        assert [1, 2, 3] as String[] == [1, 2, 3].stream().map(String::valueOf).toArray(String[]::new)
        def max = reli.collect { it.index }.inject(0) { maxIt, it -> it > maxIt ? it : maxIt }
        def min = reli.collect { it.index }.inject(0) { minIt, it -> it < minIt ? it : minIt }
        def area_data = reli.collectEntries { [it.xy, max == min ? 0 : (it.index - min) * 100 / (max - min)] }
        [
                "data_size": reli.size(),
                "area_data": area_data,
                "max_index": max,
                "min_index": min
        ]
        def maxWithStream = reli.stream()
                .map { it -> Double.valueOf(it.index) }
                .max(Comparator.comparingDouble { a -> a }).get()
        println maxWithStream
    }

    static def testComplexSwitchCase() {
        def i = 3
        def result = switch (i) {
            case 0 -> 'zero'
            case 1 -> 'one'
            case 2 -> 'two'
            case 3 -> {
                def p1 = 'th'
                def p2 = 'ree'
                "${p1}${p2}"
            }
            default -> throw new IllegalStateException('unknown number')
        }
        assert 'three' == result
    }

    static def testRange() {
        assert [2, 3, 4, 5, 6] == 1<..6   // 新增左开右闭区间
        assert [2, 3, 4, 5] == 1<..<6     // 新增开区间
        assert [1, 2, 3, 4, 5] == 1..<6   // 已有左闭右开区间
        assert [1, 2, 3, 4, 5, 6] == 1..6 // 已有闭区间
    }


}

